package com.blog.reception;

import com.blog.controller.BaseController;
import com.blog.kit.StringKit;
import com.blog.model.Album;
import com.blog.model.Blog;
import com.jfinal.plugin.activerecord.Page;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${wangjian} on 2017/9/5.
 */

public class ReceptionController extends BaseController {

    public static final ReceptionService service = new ReceptionService();

    /**
     * 首页
     */
    public void index() {
        setAttr("blog", service.getByBlog());
        setAttr("click", service.queryClick(25));
        setAttr("listKey", service.listKeywords());
        renderTemplate("index.html");
    }

    /**
     * 标签分类页面
     *
     */
    public void keywords() {
        setAttr("keywords", service.getByKeywords(getPara("key")));
        setAttr("click", service.queryClick(25));
        setAttr("listKey", service.listKeywords());
        renderTemplate("keywords.html");
    }

    /**
     * 文章列表
     */
    public void content() {
        setAttr("blog", service.paginate(getParaToInt("number", 1)));
        setAttr("click", service.queryClick(25));
        setAttr("listKey", service.listKeywords());
        renderTemplate("blog.html");
    }

    /**
     * blog详情内容
     */
    public void blogSingle(){
        Integer logId  = getParaToInt("logId");
        setAttr("blog", service.queryContent(logId));
        setAttr("click", service.queryClick(25));
        setAttr("listKey", service.listKeywords());
        setAttr("lower",service.getBlogLower(logId));
        setAttr("upper", service.getBlogUpper(logId)); // TODO  优化一下sql写法，将两个控制页面合并
        renderTemplate("blogSingle.html");
    }

    /**
     * 标签详细内容
     */
    public void single() {
        Integer logId  = getParaToInt("logId");
        setAttr("blog", service.queryContent(logId));
        setAttr("click", service.queryClick(25));
        setAttr("listKey", service.listKeywords());
        setAttr("upper", service.getUpper(logId,getPara("key")));
        setAttr("lower",service.getLower(logId,getPara("key")));
        renderTemplate("single.html");
    }

    /**
     * idea列表
     */
    public void idea() {
        setAttr("idea", service.paginateIdea(getParaToInt("number", 1)));
        setAttr("click", service.queryClick(25));
        List<Blog> listKey = service.listKeywords();
        setAttr("listKey", listKey);
        renderTemplate("idea.html");
    }

    /**
     * 相册列表
     */
    public void album() {
        Page<Album> album1 = service.page(getParaToInt("number", 1));
        setAttr("album", album1);
        setAttr("click", service.queryClick(25));
        setAttr("listKey", service.listKeywords());
        renderTemplate("album.html");
    }

    /**
     * 留言列表
     */
    public void message() {
        setAttr("message", service.paginateMessage(getParaToInt("number", 1)));
        setAttr("click", service.queryClick(25));
        setAttr("listKey", service.listKeywords());
        try {
            String a = URLDecoder.decode(getPara("name"), "utf-8");
            String b = URLDecoder.decode(getPara("edit"), "utf-8");
            setAttr("edit", b );
            setAttr("name", a );
        } catch (Exception e) {
        }
        renderTemplate("message.html");
    }

    /**
     * 添加留言
     */
    public void addMsg() {
        String edit = getPara("edit");
        String name = getPara("name");
        if (StringKit.notBlank(edit) && StringKit.notBlank(name)) {
            service.addmeg(edit, name);
            redirect("/blog/reception/message");
        } else {
            try {
                name = URLEncoder.encode(name, "utf-8");
                edit = URLEncoder.encode(edit, "utf-8");
            } catch (Exception e) {
            }
            redirect("/blog/reception/message?name=" + name + "&edit=" + edit);
        }
    }
}
